package com.bdzl.ecs.controller;

import com.alibaba.fastjson.JSONObject;
import com.bdzl.ecs.config.Constant;
import com.bdzl.ecs.pojo.SignatureVO;
import com.bdzl.ecs.servicefeign.FddCaEntFeignService;
import com.bdzl.ecs.util.contractUtils.ContractUtils;
import com.bdzl.ecs.util.electronicUtils.FddClientBaseUtils;
import com.bdzl.ecs.util.electronicUtils.FileBase64Util;
import com.bdzl.pub.entity.data.FddCaEntDO;
import com.bdzl.pub.util.file.OssUploadFileUtil;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 电子签章
 * @创建人 zhaochunyu
 * @创建时间 2021年4月27日17:03:57
 * @描述 定位系统
 */
@Controller
@RequestMapping("/bdzl/api")
@CrossOrigin("*") //处理跨域请求
public class ElectronicController {

    @Autowired
    private FddCaEntFeignService fddCaEntFeignService;

    /**
     * 添加电子签章
     * @Date 2021年4月27日17:18:09
     * @param file 文件
     * @param customerId 客户编号
     * @return {"code":1,"data":{"signature_id":"4996693","signature_sub_info":null,"status":null},"msg":"success"}  signature_id String 可选 签章图片ID signature_sub_info String 可选 扩展信息，目前为空
     * @throws IOException
     */
    @PostMapping(value = "/upload")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file, @RequestParam("customerId") String customerId,@RequestParam("fileBase64") String fileBase64 ) throws IOException {
        String base64 = FileBase64Util.getFileBase64(file);
        FddClientBaseUtils fdd123 = new FddClientBaseUtils(Constant.FDD_APPID, Constant.FDD_APPSECRET, Constant.FDD_VERSION, Constant.FDD_CAURL);
        String respons = fdd123.addSignature(customerId, base64);
        JSONObject jsonObject = JSONObject.parseObject(respons);
        if("success".equals(jsonObject.get("msg"))){
            JSONObject data = JSONObject.parseObject(jsonObject.get("data").toString());
            String docUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(fileBase64, "loan/" +"contractUtils");
            Map map = new HashMap<>(1);
            map.put("pceCustomerId",customerId);
            List<FddCaEntDO> fddCaEntDOList = fddCaEntFeignService.Bylist(map);
            for (FddCaEntDO fddCaEntDO:fddCaEntDOList) {
                fddCaEntDO.setPecSealPic(docUrl);//pecSealPic
                fddCaEntDO.setPecSealFdd(data.get("signature_id").toString());
                fddCaEntFeignService.update(fddCaEntDO);
            }
        }
        return respons;
    }

    /**
     * 查询电子签章
     * @param signatureVO
     * @return
     */
    @RequestMapping("/querySignature")
    @ResponseBody
    public String querySignature(@RequestBody SignatureVO signatureVO) {
        FddClientBaseUtils fdd123 = new FddClientBaseUtils(Constant.FDD_APPID, Constant.FDD_APPSECRET, Constant.FDD_VERSION, Constant.FDD_CAURL);
        String response = fdd123.querySignature(signatureVO.getCustomerId(), signatureVO.getSignatureId());
        JSONObject jsonObject = JSONObject.parseObject(response);
        return response;
    }

    //替换
    @PostMapping("/uploadadd")
    @ResponseBody
    public String uploadadd(@RequestBody SignatureVO signatureVO) throws IOException {
        FddClientBaseUtils fdd123 = new FddClientBaseUtils(Constant.FDD_APPID, Constant.FDD_APPSECRET, Constant.FDD_VERSION, Constant.FDD_CAURL);
        String response = fdd123.replaceSignature(signatureVO.getCustomerId(), signatureVO.getSignatureId(), signatureVO.getSignatureImgBase64());
        return response;
    }

    /**
     * 删除印章接口
     * @return
     */
    @ResponseBody
    @RequestMapping("/removeSignature")
    public String RemoveSignature(@RequestBody SignatureVO signatureVO){
        String response= ContractUtils.RemoveSignature(signatureVO.getCustomerId(),signatureVO.getSignatureId());
        return response;
    }

    /**
     * 设置默认印章接口 注：需要企业客户id
     * @return
     */
    @ResponseBody
    @RequestMapping("/defaultSignature")
    public String DefaultSignature(@RequestBody SignatureVO signatureVO){
        String response= ContractUtils.DefaultSignature(signatureVO.getCustomerId(),signatureVO.getSignatureId());
        return response;
    }
}